var Module = require("../../../class/admin/module.c");
var Post = require("../../../class/admin/post.c");
var Reply = require("../../../class/_include/reply.c");

var _Module = new Module();
var _Post = new Post();
// var _Reply = new Reply();
module.exports = {
    prefix: "/admin/module/",
    post: {
        "create": {
            des: {
                name: "createModule",
                content: "创建板块",
                params: [
                    {
                        key: "name",
                        type: "string"
                    }
                ]
            },
            method: function* (next) {
                var self = this;
                print("创建新版块");
                try {
                    var data = yield parse(self.request);
                    data ? true : (self.body = printError("没有信息"));
                    var _mod = yield new Module(data, self.session.admin).save();
                    self.body = _mod;
                    yield next;
                } catch (e) {
                    self.body = printError(e);
                };
                self.body = data;
            }
        },
    },
    get: {
        "listbypage": {
            des: {
                name: "getModuleListByPageInfo",
                content: "根据分页信息获取板块列表",
                params: [
                    {
                        key: "page",
                        type: "Number"
                    },
                    {
                        key: "num",
                        type: "Number"
                    }
                ]
            },
            method: function* () {
                var self = this;
                print("根据分页信息获取板块列表");
                try {
                    data = self.request.query;
                    console.log(data)
                    var _list = yield _Module.getListByPage(data);
                    self.body = _list;
                } catch (e) {
                    self.body = printError(e);
                };
            }
        },
        "listall": {
            des: {
                name: "getModuleListAll",
                content: "获取所有版块",
                params: []
            },
            method: function* () {
                var self = this;
                print("获取所有版块");
                try {
                    var _module_list = yield _Module.getAll();
                    self.body = _module_list;
                } catch (e) {
                    self.body = printError(e);
                };
            }
        },
        "detail/:_id": {
            des: {
                name: "getModuleDetail",
                content: "获取版块详情",
                params: [
                    {
                        params: "_id",
                        type: "string"
                    }
                ]
            },
            method: function* () {
                var self = this;
                print("获取版块详情");
                try {
                    var _id = self.params._id;
                    var _detail = yield _Module.detail(_id);
                    self.body = _detail;
                } catch (e) {
                    self.body = printError(e);
                }
            }
        },
        "count": {
            des: {
                name: "moduleCount",
                content: "版块统计",
                params: []
            },
            method: function* () {
                var self = this;
                print("获取版块统计");
                try {
                    var _count = yield _Module.count();
                    console.log(_count);
                    self.body = _count;
                } catch (e) {
                    self.body = printError(e);
                };
            }
        }
    },
    put: {
        "update": {
            des: {
                name: "moduleUpdate",
                content: "更新版块信息",
                params: [
                    {
                        key: "module",
                        type: "Module"
                    }
                ]
            },
            method: function* () {
                var self = this;
                print("更新版块信息");
                try {
                    var data = yield parse(self.request);
                    var _updated_module = yield _Module.update(data);
                    self.body = _updated_module;
                } catch (e) {
                    self.body = printError(e);
                };
            }
        }
    },
    delete: {
        "delete/:_id": {
            des: {
                name: "moduleDelete",
                content: "删除指定版块",
                params: [
                    {
                        params: "_id",
                        type: "Number|String"
                    }
                ]
            },
            method: function* () {
                var self = this;
                print("删除指定板块");
                try {
                    var _id = self.params._id;
                    var _mod = yield _Module.delete(_id);
                    self.body = _mod;
                    // 删除版块后，删除对应的话题和回复
                    if (_mod.type !== "error") {
                        yield _Post.deleteByModule(_id);
                        yield _Reply.deleteByModule(_id);
                    };
                } catch (e) {
                    self.body = printError(e);
                };
            }
        }
    }
}